package problem606;

//606.根据二叉树创建字符串
//https://leetcode.cn/problems/construct-string-from-binary-tree/description/

class Solution {
    StringBuffer str = new StringBuffer();
    public String tree2str(TreeNode root) {
        createStr(root);
        return str.toString();
    }
    private void createStr(TreeNode root) {
        str.append(root.val);
        if(root.left == null && root .right != null) {
            str.append("()");
        }
        if(root.left!=null) {
            str.append('(');
            createStr(root.left);
            str.append(')');
        }
        
        if(root.right != null) {
            str.append('(');
            createStr(root.right);
            str.append(')');
        }
    }
}